package com.playcrab.adouban;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gdata.client.douban.DoubanService;
import com.google.gdata.data.douban.Attribute;
import com.google.gdata.data.douban.ReviewEntry;
import com.google.gdata.data.douban.ReviewFeed;
import com.google.gdata.data.douban.SubjectEntry;
import com.google.gdata.data.douban.SubjectFeed;
import com.google.gdata.util.ServiceException;
import com.playcrab.adouban.util.DoubanAPI;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class ViewBook extends ViewSubject {

	private static final String LOG_TAG = "ViewBook";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle bundle = this.getIntent().getExtras();
		id = bundle.getString("id");
		type = bundle.getString("type");
		Log.v(LOG_TAG, "id:" + id);
		//setContentView(R.layout.view_book);
		this.showPending();

		new Thread() {
			public void run() {
				try {
					ViewBook.this.getSubject();
					handler.post(new Runnable() {
						public void run() {
							ViewBook.this.showSubject();
							ViewBook.this.showReview();
							ViewBook.this.hidePending();
						}
					});
				} catch (IOException e) { 
					e.printStackTrace(); 
					ViewBook.this.showInfo(getString(R.string.network_error));
				} catch (ServiceException e) {
					e.printStackTrace();
					ViewBook.this.showInfo(getString(R.string.service_error));
				}finally {
					
				}
			}

		}.start();

	}

	@Override
	public SubjectEntry getSubject() throws IOException, ServiceException {
		DoubanService service = DoubanAPI.getInstance().getService();
		subject = service.getBook(id);
		reviewFeed = service.getBookReviews(id, 1, 5, "score");
		return subject;
	}

	@Override
	public void showSubject() {
		if (subject == null) {
			subjectHeader = null;
			return;
		}

		LayoutInflater inflater = LayoutInflater.from(this);
		subjectHeader = inflater.inflate(R.layout.view_book, null);
		
		if (subject.getTitle() != null) {
			TextView title = (TextView) subjectHeader.findViewById(R.id.book_title);
			title.setText(subject.getTitle().getPlainText());
		}

		LinearLayout book_info = (LinearLayout) subjectHeader.findViewById(R.id.book_info);
		book_info.addView(newRatingView());

		Map<String, List<String>> attributes = DoubanAPI.getInstance()
				.buildAttributes(subject.getAttributes());

		book_info.addView(newAttributeView(attributes, "author", "作者:  "));
		book_info.addView(newAttributeView(attributes, "publisher", "出版社:  "));

		book_info.addView(newAttributeView(attributes, "pubdate", "出版日期:  "));
		book_info.addView(newAttributeView(attributes, "isbn10", "ISBN10:  "));

		book_info.addView(newAttributeView(attributes, "isbn13", "ISBN13:  "));
		book_info.addView(newAttributeView(attributes, "pages", "页数:  "));
		book_info.addView(newAttributeView(attributes, "price", "定价:  "));

		if (subject.getSummary() != null) {
			((TextView) subjectHeader.findViewById(R.id.book_summary)).setText(subject
					.getSummary().getPlainText());
		}

		if (subject.getLink("image", null) != null) {
			WebView image = (WebView) subjectHeader.findViewById(R.id.book_image);
			image.loadUrl(subject.getLink("image", null).getHref());
		}
		
	}

}
